package com.wistech.search.solr.extend;

import java.util.Map;

import org.springframework.beans.factory.annotation.Required;

import com.google.common.collect.Maps;
import com.wistech.search.solr.extend.model.Company;
import com.wistech.search.solr.extend.model.Product;
import com.wistech.search.solr.extend.service.CompanyManager;
import com.wistech.search.solr.extend.service.ProductManager;

/**
 * 产品评分处理器
 * 
 * @author Liuye
 * 
 */
public class ProductScoreHandler extends ScoreHandler<Product> implements
		DocumentHander {
	private ProductManager productManager;
	private CompanyManager companyManager;
	private String fk = "companyId";

	@Override
	public Product getEntity(String id) {
		return productManager.getProduct(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.wistech.search.solr.extend.ScoreHandler#getRelationData(java.util
	 * .Map)
	 */
	@Override
	protected Map<String, Object> getRelationData(Map<String, Object> model) {
		// 通过userId关联获取公司对象
		Map<String, Object> relationModel = Maps.newHashMap();
		relationModel.put("c", new Company());
		Object obj = model.get(fk);
		if (obj != null) {
			String companyId = String.valueOf(obj);
			if (companyId != null) {
				Company company = companyManager.getCompany(companyId);
				if (company != null) {
					relationModel.put("c", company);
				}
			}
		}
		return relationModel;
	}

	/**
	 * @param productManager
	 *            the productManager to set
	 */
	@Required
	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}

	/**
	 * @param companyManager
	 *            the companyManager to set
	 */
	public void setCompanyManager(CompanyManager companyManager) {
		this.companyManager = companyManager;
	}
}
